package cz.czu.pef.DataModelling;

import java.util.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

@Entity
@Table(name="TRAINING_TYPES")
@NamedQueries({
	@NamedQuery(name="findTrainingTypeByCode", query="SELECT tt FROM TrainingType tt WHERE tt.code = :code")
})
@ManagedBean
@ViewScoped
@Component
public class TrainingType extends CommonPersistenceObject
{
	private static final long serialVersionUID = 4358442447516594030L;
	
	private long defaultPeriodInSeconds;

	@Column(nullable=true)
	private String evidencePassName;

	@OneToMany(fetch=FetchType.LAZY, targetEntity=Training.class, mappedBy="trainingType") // no cascade!
	//@OnDelete(action=OnDeleteAction.NO_ACTION)
	private Set<Training> trainings;

	@OneToMany(fetch=FetchType.LAZY, targetEntity=TrainingTypeForRoleType.class, mappedBy="trainingType") // no cascade!
	private Set<TrainingTypeForRoleType> trainingTypeForRoleTypes;

	private TrainingType()
	{
		super(null, null, null);
	}

	public TrainingType(String trainingCode, String trainingName, String trainingDescription, String evidencePassName, long defaultPeriodInSeconds)
	{
		super(trainingCode, trainingName, trainingDescription);
		this.evidencePassName = evidencePassName;
		this.defaultPeriodInSeconds = defaultPeriodInSeconds;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<Training> getTrainings()
	{
		return trainings;
	}

	
	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<TrainingTypeForRoleType> getTrainingTypeForRoleTypes()
	{
		return trainingTypeForRoleTypes;
	}

	
	public long getDefaultPeriodInSeconds()
	{
		return defaultPeriodInSeconds;
	}

	
	public void setDefaultPeriodInSeconds(long defaultPeriodInSeconds)
	{
		this.defaultPeriodInSeconds = defaultPeriodInSeconds;
	}

	
	public String getEvidencePassName()
	{
		return evidencePassName;
	}

	
	public void setEvidencePassName(String evidencePassName)
	{
		this.evidencePassName = evidencePassName;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("TrainingType [defaultPeriodInSeconds=");
		builder.append(defaultPeriodInSeconds);
		builder.append(", evidencePassName=");
		builder.append(evidencePassName);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/**
	 * Can be deleted when all referencing entities are also deleted (no cascade).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getTrainingTypeForRoleTypes().size() == 0 && getTrainings().size() == 0;
	}
}
